//
//  LBMStatusViewModel.swift
//  微博
//
//  Created by 刘保满 on 16/5/24.
//  Copyright © 2016年 itheima. All rights reserved.
// cell 的视图模型

import UIKit

class LBMStatusViewModel: NSObject {

    //模型属性
    var status: LBMStatus?
 
    //模型中的mbrank 不能直接用于视图显示,需要处理
    var mbImage: UIImage?
    
    //认证
    var verifiedImage: UIImage?
    
    //头像请求的URL
    var headImageURL: NSURL?

    //微博来源
    var source: String?
    
    //微博时间
    var time: String? {
        
        return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
 
    }
    
    //转发
    var reponstsText: String?
    //评论
    var commentsText: String?
    //赞
    var attitudesText: String?

    //原创微博正文的属性文本
    var originalAttributeString: NSAttributedString?
    
    
    
    init(status: LBMStatus) {
        super.init()
        //赋值
        self.status = status
        //处理头像url
        headImageURL = NSURL(string: status.user?.profile_image_url ?? "")
        //处理用户等级
        dealmbRank()
        //用户认证
        dealVerifiedType()
        //微博来源
        dealWeiboSource()
        //底部工具条文字
        reponstsText = dealWeiboToolBarText(status.reposts_count, defaultText: "转发")
        commentsText = dealWeiboToolBarText(status.comments_count, defaultText: "评论")
        attitudesText = dealWeiboToolBarText(status.attitudes_count, defaultText: "赞")
        
        
    }
    

    
}



//处理微博数据
extension LBMStatusViewModel {
    //用户等级
    private func dealmbRank() {
        let mbType = status?.user?.mbrank ?? 0
        if mbType > 0 && mbType < 7 {
            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
        }

    }
    
    //用户认证
    private func dealVerifiedType() {
        let verifiedType = status?.user?.verified ?? -1
        
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
            
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
            
        default:
            verifiedImage = nil
        }
        
        
    }

    //微博来源
    private func dealWeiboSource() {
        
        let startFlag = "\">"
        let endFlag = "</"
        let str = status?.source ?? ""
        if let startRange = str.rangeOfString(startFlag), endRange = str.rangeOfString(endFlag) {
            
            let range = startRange.endIndex..<endRange.startIndex
            
            source = str.substringWithRange(range)
            
        }

    }
    //处理底部工具条显示的文字
    //如果没有评论,转发或赞,就显示默认的文字
    //如果有,就显示相关的数据,如果超过一万条就要显示万条
      func dealWeiboToolBarText(count: Int, defaultText: String) -> String {
        
        if count <= 0 {
            //显示默认的文字
            return defaultText
        }
        
        if count >= 10000 {
            //如 11000  1.1万
            return "\(Double(count / 1000) / 10)万"
        }
        
        return "\(count)"
        
    }
    
    
    
    
    
}























